const http=require('http')
const  fs=require('fs')
const url=require('url')

const path=require('path')
const server=http.createServer((req,res)=>{
    res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
    // res.setHeader("Content-Type", "application/json;charset=utf-8");

// let myurl=req.url
//   console.log(req.url)

// console.log(myurl)
//    let obj= url.parse(req.url);
//    console.log(obj)
//    console.log(req.method)

let  obj=url.parse(req.url,true);
//   console.log(obj)
  let query=obj.query
  let pathname=obj.pathname

   


   if(pathname=='/news'&&pathname!=='/favicon.ico' ) {
       res.end('aaa页面')
   } else if(pathname=='/books' &&pathname!=='/favicon.ico') {
       res.end('book要么')
   }else if(pathname=='/home'&& pathname!=='/favicon.ico') {
       console.log(1)
    //    let query={};
    // console.log(query)
    //    let obj=query;
    //    console.log(obj)

       if(query.name) {
        res.write(JSON.stringify(query))
       }else {
           res.write('请输入参数')
       }
       
          
          res.end()
   }




  

//    fs.readFile('第一个参数文件的路径 写绝对路径','第二个参数编码格式utf8','第三个参数回调函数')

// let url=path.resolve(__dirname,'./1.html');

    // fs.readFile(url,'utf8',(err,data)=>{
    //     if(err) throw  new Error(err)
    //     res.write(data)
    //     res.end()           
        
    // })
   
})




server.listen(3000,()=>{
    console.log('3000 启动')
})